<?php
namespace Home\Controller;
use Think\Controller;
class ServiceController extends ZMController {
	public function getException(){//显示异常
		$Model=M()->db(1,'DB_CONFIG1');
		$hid=I('hid');
		$page=I('page');
		$limit=I('limit');
		$start=($page-1)*$limit;
		if(!is_numeric($start)){
            $start = 0;
        }
		if(!is_numeric($limit)){
            $limit = 999;
        }
		$limit=" limit $start,$limit";
		$r=$Model->query("select id from ddwk_exception where hid=$hid");
		$res=$Model->query("select a.id,a.pdate,a.type,a.sno,a.exception as result,a.context,a.flag as isover,b.name as cname,c.name as sname from ddwk_exception as a left join ddwk_employee as b on a.empid=b.id left join ddwk_employee as c on a.sid=c.id where a.hid=$hid order by a.pdate desc $limit");
		if(empty($res)){
			$res=array();
		}
		$this->ajax($res,count($r));
	}
	public function saveException(){//登记异常  0遗失  1泄漏
		$Model=M()->db(1,'DB_CONFIG1');
		$id=I('id');
		$data['pdate']=date('Y-m-d',strtotime(I('pdate')));
		$data['type']=I('type');
		if(session('?user.hid')){
		    $data['hid']=session('user.hid');
		}
		else{
			$data['hid']=I('hid');
		}
		$data['sno']=I('sno');
		$data['empid']=I('cname');
		$data['context']=I('context');
		if(empty($id)){
			$res=$Model->table('ddwk_exception')->add($data);
		}
		else{
			$res=$Model->table('ddwk_exception')->where("id=$id")->field('version')->find();
			$version=$res['version'];
			$d['version']+=1;
			$d['exception']=I('result');
			$d['flag']=1;
			$d['sid']=session('user.userid');
			$res=$Model->table('ddwk_exception')->where("id=$id and version=$version")->save($d);
		}
		if($res!==false){
			$this->successreturn('ok');
		}
		else{
			$this->errorreturn('error');
		}
	}
}
?>
